/*
 Copyright aswing.org, see the LICENCE.txt.
*/

package com.UI { 

	import com.*;
	import com.Engine.*;
	import com.Packet.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.external.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.net.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	
	import org.aswing.*;
	import org.aswing.border.*;
	import org.aswing.border.EmptyBorder;
	import org.aswing.colorchooser.*;
	import org.aswing.ext.*;
	import org.aswing.geom.*;
	import org.aswing.plaf.*;
	import org.aswing.skinbuilder.SkinProgressBarBackground;
	import org.aswing.skinbuilder.SkinProgressBarForeground;
	
	import flash.text.TextField;
	import org.aswing.border.EmptyBorder;
	import org.aswing.event.*;
	import org.aswing.geom.IntPoint;


public class FrameInfo extends JPanel {
	public static var OK_STR:String = "OK";
	public static var CANCEL_STR:String = "Cancel";
	public static var YES_STR:String = "Yes";
	public static var NO_STR:String = "No";
	public static var CLOSE_STR:String = "Close";
	
	
	public static const OK:int = 1; //00001
	public static const CANCEL:int = 2; //00010
	public static const YES:int = 4; //00100
	public static const NO:int = 8; //01000
	public static const CLOSE:int = 16; //10000
	
	private var okButton:JButton;
	private var cancelButton:JButton;
	private var yesButton:JButton;
	private var noButton:JButton;
	private var closeButton:JButton;
	
	private var centerPane:JPanel;
	private var msgLabel:JTextArea;
	private var inputText:JTextField;
	private var buttonPane:JPanel;
	private var frame:JFrame;
	//-----------------------------------
	public var btn_close:JButton;
	public function FrameInfo() {
		
		/*super(new BorderLayout());
		centerPane = SoftBox.createVerticalBox(6);
		msgLabel = new JLabel();
		centerPane.append(AsWingUtils.createPaneToHold(msgLabel, new FlowLayout(FlowLayout.CENTER, 5, 5)));
		inputText = new JTextField();
		var inputContainer:JPanel = new JPanel(new BorderLayout());
		inputContainer.setBorder(new EmptyBorder(null, new Insets(0, 8, 0, 8)));
		inputContainer.append(inputText, BorderLayout.CENTER);
		centerPane.append(inputContainer);
		buttonPane = new JPanel(new FlowLayout(FlowLayout.CENTER));
		append(centerPane, BorderLayout.CENTER);
		append(buttonPane, BorderLayout.SOUTH);*/
		
		super(new BorderLayout());
		centerPane = SoftBox.createVerticalBox(6);
		msgLabel = new JTextArea();
		centerPane.append(AsWingUtils.createPaneToHold(msgLabel, new FlowLayout(FlowLayout.CENTER, 5, 5)));
		inputText = new JTextField();
		var inputContainer:JPanel = new JPanel(new BorderLayout());
		inputContainer.setBorder(new EmptyBorder(null, new Insets(0, 8, 0, 8)));
		inputContainer.append(inputText, BorderLayout.CENTER);
		centerPane.append(inputContainer);
		buttonPane = new JPanel(new FlowLayout(FlowLayout.CENTER));
		buttonPane.setPreferredHeight(48);
		append(centerPane, BorderLayout.CENTER);
		append(buttonPane, BorderLayout.SOUTH);
		
		var filter:GlowFilter;
		var font:ASFont = new ASFont("宋体", 13, false, false, false, false);
		filter = new GlowFilter(0x000000, 1, 2, 2, 10);
		
		msgLabel.setFont(font);
		msgLabel.setForeground(ASColor.WHITE);
		msgLabel.filters = (new Array(filter));
		msgLabel.setEditable(false);
		//msgLabel.setWordWrap(true);
		msgLabel.setBorder(new EmptyBorder);
		inputText.setBorder(new EmptyBorder);
		//inputText.setPreferredHeight(17);
		
		msgLabel.setBackgroundDecorator(null);
	}
	
	public function getFrame():JFrame{
		return frame;
	}
	
	public function getInputText():JTextField{
		return inputText;
	}
	
	public function getMsgLabel():JTextArea{
		return msgLabel;
	}
	
	public function getOkButton():JButton{
		if(okButton == null){
			okButton = new JButton(OK_STR);
		}
		return okButton;
	}
	
	public function getCancelButton():JButton{
		if(cancelButton == null){
			cancelButton = new JButton(CANCEL_STR);
		}
		return cancelButton;
	}	
	public function getYesButton():JButton{
		if(yesButton == null){
			yesButton = new JButton(YES_STR);
		}
		return yesButton;
	}
	
	public function getNoButton():JButton{
		if(noButton == null){
			noButton = new JButton(NO_STR);
		}
		return noButton;
	}	
	public function getCloseButton():JButton{
		if(closeButton == null){
			closeButton = new JButton(CLOSE_STR);
		}
		return closeButton;
	}	
	
	public function addButton(button:JButton):void{
		buttonPane.append(button);
	}
	private function setMessage(msg:String):void{
		msgLabel.setText(msg);
	}
	private function setIcon(icon:Icon):void{
		//msgLabel.setIcon(icon);
	}	
	private function addCloseListener(button:JButton):void{
		var f:JFrame = getFrame();
		button.addActionListener(function():void{ f.tryToClose(); });
	}
	
	/**
	 * showMessageDialog(title:String, msg:String, finishHandler:Function, parentComponent:Component, modal:Boolean, icon:Icon, buttons:Number)<br>
	 * showMessageDialog(title:String, msg:String, finishHandler:Function, parentComponent:Component, modal:Boolean, icon:Icon)<br>
	 * showMessageDialog(title:String, msg:String, finishHandler:Function, parentComponent:Component, modal:Boolean)<br>
	 * showMessageDialog(title:String, msg:String, finishHandler:Function, parentComponent:Component)<br>
	 * showMessageDialog(title:String, msg:String, finishHandler:Function)
	 * <p>
	 * Show a message box with specifield title, message, and icon.
	 * <p>
	 * for example:
	 * <pre>
	 * var handler:Function = __whenUserConformed;
	 * var pane:JOptionPane = showMessageDialog("title", "is that OK?", handler, null, JOptionPane.YES|JOptionPane.NO);
	 * </pre>
	 * will show a message box with yes and no buttons.
	 * 
	 * @param title the title of the box, can be null
	 * @param msg the message , can be null
	 * @param finishHandler the function(result:Number) to call when user finished input, 
	 * will pass a number as parammeter, its value indicate what user's selection. 
	 * For example CANCEL indicate that user pressed CANCEL button, CLOSE indicate that 
	 * user pressed CLOSE button or just closed the frame by click frame's close button, 
	 * YES indicate that user pressed the YES button etc.
	 * @param parentComponent determines the Frame in which the
	 *		dialog is displayed; can be null
	 * @param modal is the window modal, default is true
	 * @param icon the icon, can be null
	 * @param buttons which buttons need to show.
	 * @param closeBox is close the box frame when user click buttons
	 * @see #OK
	 * @see #CANCEL
	 * @see #YES
	 * @see #NO
	 * @see #CLOSE
	 */
	public static function showMessageDialog(title:String, msg:String, finishHandler:Function=null, parentComponent:Component=null, modal:Boolean=true, icon:Icon=null, buttons:int=OK):FrameInfo{
		var frame:JFrame = new JFrame(AsWingUtils.getOwnerAncestor(parentComponent), title, modal);
		var pane:FrameInfo = new FrameInfo();
		pane.getInputText().setVisible(false);
		pane.setMessage(msg);
		pane.setIcon(icon);
		pane.frame = frame;
		var handler:Function = finishHandler;
		if((buttons & OK) == OK){
			pane.addButton(pane.getOkButton());
			pane.addCloseListener(pane.getOkButton());
			pane.getOkButton().addActionListener(function():void{
				if (handler != null) handler(FrameInfo.OK);
			});
		}
		if((buttons & YES) == YES){
			pane.addButton(pane.getYesButton());
			pane.addCloseListener(pane.getYesButton());
			pane.getYesButton().addActionListener(function():void{
				if (handler != null) handler(FrameInfo.YES);
			});
		}
		if((buttons & NO) == NO){
			pane.addButton(pane.getNoButton());
			pane.addCloseListener(pane.getNoButton());
			pane.getNoButton().addActionListener(function():void{
				if (handler != null) handler(FrameInfo.NO);
			});
		}
		if((buttons & CANCEL) == CANCEL){
			pane.addButton(pane.getCancelButton());
			pane.addCloseListener(pane.getCancelButton());
			pane.getCancelButton().addActionListener(function():void{
				if (handler != null) handler(FrameInfo.CANCEL);
			});
		}
		if((buttons & CLOSE) == CLOSE){
			pane.addButton(pane.getCloseButton());
			pane.addCloseListener(pane.getCloseButton());
			pane.getCloseButton().addActionListener(function():void{
				if (handler != null) handler(FrameInfo.CLOSE);
			});
		}
		frame.addEventListener(FrameEvent.FRAME_CLOSING, 
			function():void{
				if (handler != null) handler(FrameInfo.CLOSE);
			});
		
		pane.btn_close = new JButton();
		pane.btn_close.setSize(new IntDimension(27, 27));
		pane.btn_close.setPreferredSize(new IntDimension(27, 27));
		frame.getTitleBar().setCloseButton(pane.btn_close);
		frame.getTitleBar().setMinimizeHeight(32);
		pane.btn_close.wrapSimpleButton(Client.getUIElement("btn_close") as SimpleButton);
		pane.btn_close.setText("");
		
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setResizable(false);
		frame.getContentPane().append(pane, BorderLayout.CENTER);
		frame.pack();
		var location:IntPoint = AsWingUtils.getScreenCenterPosition();
		location.x = Math.round(location.x - frame.getWidth()/2);
		location.y = Math.round(location.y - frame.getHeight()/2);
		frame.setLocation(location);
		frame.show();
		
		var img_background:DisplayObject = Client.getUIElement("img_info_bg") as DisplayObject
		pane.getFrame().setBackgroundDecorator(new AssetBackground(img_background));
		pane.getFrame().setPreferredWidth(img_background.width);
		pane.getFrame().setPreferredHeight(img_background.height);
		pane.getFrame().setLocationXY((Engine.W - img_background.width) / 2,(Engine.H - img_background.height)/2);
		pane.getMsgLabel().setPreferredWidth(img_background.width - 32);
		pane.getMsgLabel().setWordWrap(true);
		
		//var border:LineBorder = new LineBorder(null, new ASColor(0x66CCFF,1),.1,15);
		pane.getFrame().setForeground(ASColor.WHITE);
		pane.getFrame().getContentPane().setForeground(ASColor.WHITE);
		pane.getFrame().setBorder(new EmptyBorder);
		pane.getFrame().setClosable(true);
		pane.getFrame().setBackground(new ASColor(0x68878e,1.0));
		pane.getFrame().setDragable(true);
		pane.getFrame().getTitleBar().setMinimizeHeight(32);
		pane.getOkButton().wrapSimpleButton(Client.getUIElement("btn_info_ok") as SimpleButton);
		pane.getOkButton().setText("");
		pane.getCancelButton().wrapSimpleButton(Client.getUIElement("btn_info_cancel") as SimpleButton);
		pane.getCancelButton().setText("");
		pane.getYesButton().wrapSimpleButton(Client.getUIElement("btn_info_yes") as SimpleButton);
		pane.getYesButton().setText("");
		pane.getNoButton().wrapSimpleButton(Client.getUIElement("btn_info_no") as SimpleButton);
		pane.getNoButton().setText("");
		pane.getMsgLabel().setForeground(ASColor.WHITE);
		var filter:GlowFilter;
		var font:ASFont = new ASFont("宋体", 13, false, false, false, false);
		filter = new GlowFilter(0x000000, 1, 2, 2, 10);
		pane.getMsgLabel().setFont(font);
		pane.getMsgLabel().filters = new Array(filter);
		frame.pack();
		return pane;
	}
	
	/**
	 * showMessageDialog(title:String, msg:String, finishHandler:Function, defaultValue:String, parentComponent:Component, modal:Boolean, icon:Icon)<br>
	 * showMessageDialog(title:String, msg:String, finishHandler:Function, defaultValue:String, parentComponent:Component, modal:Boolean)<br>
	 * showMessageDialog(title:String, msg:String, finishHandler:Function, defaultValue:String, parentComponent:Component)<br>
	 * showMessageDialog(title:String, msg:String, finishHandler:Function, defaultValue:String)<br>
	 * showMessageDialog(title:String, msg:String, finishHandler:Function)
	 * <p>
	 * Show a message box with specifield title, message, and icon and a TextField to requare 
	 * user to input a string.
	 * <p>
	 * for example:
	 * <pre>
	 * var handler:Function = __whenUserEntered;
	 * var pane:FrameInfo = showMessageDialog("title", "Please enter your name:", handler, "yournamehere");
	 * </pre>
	 * will show a message box with OK and CANCEL and a input Texts.
	 * 
	 * @param title the title of the box, can be null
	 * @param msg the message , can be null
	 * @param finishHandler the function(result:String) to call when user finished input, 
	 * will pass a string as parammeter, if it is null indicate that user had pressed 
	 * cancel or just closed the frame, otherwise it is the string what user entered.
	 * @param defaultValue the default value for the input
	 * @param parentComponent determines the Frame in which the
			dialog is displayed; can be null
	 * @param modal is the window modal, default is true
	 * @param icon the icon, can be null
	 */
	public static function showInputDialog(title:String, msg:String, finishHandler:Function=null, defaultValue:String="", parentComponent:Component=null, modal:Boolean=true, icon:Icon=null):FrameInfo{
		var frame:JFrame = new JFrame(AsWingUtils.getOwnerAncestor(parentComponent), title, modal);
		var pane:FrameInfo = new FrameInfo();
		if(defaultValue != ""){
			pane.inputText.setText(defaultValue);
		}
		pane.setMessage(msg);
		pane.setIcon(icon);
		pane.frame = frame;
		
		pane.addButton(pane.getOkButton());
		pane.addCloseListener(pane.getOkButton());
		pane.addButton(pane.getCancelButton());
		pane.addCloseListener(pane.getCancelButton());
		
		var handler:Function = finishHandler;
		
		pane.getOkButton().addActionListener(
			function():void{
				if (handler != null) handler(pane.getInputText().getText());
			}
		);
		
		var cancelHandler:Function = function():void{
			if (handler != null) handler(null);
		};
		
		pane.btn_close = new JButton();
		pane.btn_close.setSize(new IntDimension(27, 27));
		pane.btn_close.setPreferredSize(new IntDimension(27, 27));
		frame.getTitleBar().setCloseButton(pane.btn_close);
		frame.getTitleBar().setMinimizeHeight(32);
		pane.btn_close.wrapSimpleButton(Client.getUIElement("btn_close") as SimpleButton);
		pane.btn_close.setText("");
		
		pane.getCancelButton().addActionListener(cancelHandler);
		frame.addEventListener(FrameEvent.FRAME_CLOSING, cancelHandler);
			
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setResizable(false);
		frame.getContentPane().append(pane, BorderLayout.CENTER);
		frame.pack();
		var location:IntPoint = AsWingUtils.getScreenCenterPosition();
		location.x = Math.round(location.x - frame.getWidth()/2);
		location.y = Math.round(location.y - frame.getHeight()/2);
		frame.setLocation(location);
		frame.show();
		
		var img_background:DisplayObject = Client.getUIElement("img_info_bg") as DisplayObject
		pane.getFrame().setBackgroundDecorator(new AssetBackground(img_background));
		//pane.getFrame().setSizeWH(img_background.width,img_background.height);
		pane.getFrame().setPreferredWidth(img_background.width);
		pane.getFrame().setPreferredHeight(img_background.height);
		pane.getFrame().setLocationXY((Engine.W - img_background.width) / 2,(Engine.H - img_background.height)/2);
		pane.getMsgLabel().setPreferredWidth(img_background.width - 32);
		
		pane.getFrame().setForeground(ASColor.WHITE);
		pane.getFrame().getContentPane().setForeground(ASColor.WHITE);
		pane.getFrame().setBorder(new EmptyBorder);
		pane.getFrame().setClosable(true);
		//pane.getFrame().setBackground(new ASColor(0x68878e,1.0));
		pane.getFrame().setDragable(false);
		pane.getFrame().getTitleBar().setMinimizeHeight(32);
		pane.getOkButton().wrapSimpleButton(Client.getUIElement("btn_info_ok") as SimpleButton);
		pane.getOkButton().setText("");
		pane.getCancelButton().wrapSimpleButton(Client.getUIElement("btn_info_cancel") as SimpleButton);
		pane.getCancelButton().setText("");
		pane.getYesButton().wrapSimpleButton(Client.getUIElement("btn_info_yes") as SimpleButton);
		pane.getYesButton().setText("");
		pane.getNoButton().wrapSimpleButton(Client.getUIElement("btn_info_no") as SimpleButton);
		pane.getNoButton().setText("");
		pane.getMsgLabel().setForeground(ASColor.WHITE);
		//var filter:GlowFilter;
		//var font:ASFont = new ASFont("宋体", 12, false, false, false, false);
		//filter = new GlowFilter(0x000000, 1, 2, 2, 10);
		//pane.getMsgLabel().setFont(font);
		//pane.getMsgLabel().filters = new Array(filter);
		frame.pack();
		
		return pane;
	}
}
	
}